package com.nbcio.modules.estar.classconfig.service.impl;

import com.nbcio.modules.estar.classconfig.entity.ErpClassconfig;
import com.nbcio.modules.estar.classconfig.entity.ErpClassconfigB;
import com.nbcio.modules.estar.classconfig.mapper.ErpClassconfigBMapper;
import com.nbcio.modules.estar.classconfig.mapper.ErpClassconfigMapper;
import com.nbcio.modules.estar.classconfig.service.IErpClassconfigService;
import com.nbcio.modules.flowable.apithird.service.FlowCallBackServiceI;
import com.nbcio.modules.flowable.apithird.service.FlowCommonService;
import com.nbcio.modules.flowable.apithird.business.entity.FlowMyBusiness;
import com.nbcio.modules.flowable.utils.StatusEnum;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 商城首页配置
 * @Author: nbacheng
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service("erpClassconfigService")
public class ErpClassconfigServiceImpl extends ServiceImpl<ErpClassconfigMapper, ErpClassconfig> implements IErpClassconfigService , FlowCallBackServiceI{



    @Autowired
    FlowCommonService flowCommonService;
	@Autowired
	private ErpClassconfigMapper erpClassconfigMapper;
	@Autowired
	private ErpClassconfigBMapper erpClassconfigBMapper;


	@Override
	@Transactional
	public void saveMain(ErpClassconfig erpClassconfig, List<ErpClassconfigB> erpClassconfigBList) {
		erpClassconfigMapper.insert(erpClassconfig);
		if(erpClassconfigBList!=null && erpClassconfigBList.size()>0) {
			for(ErpClassconfigB entity:erpClassconfigBList) {
				//外键设置
				entity.setPkH(erpClassconfig.getId());
				erpClassconfigBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ErpClassconfig erpClassconfig,List<ErpClassconfigB> erpClassconfigBList) {
		erpClassconfigMapper.updateById(erpClassconfig);

		//1.先删除子表数据
		erpClassconfigBMapper.deleteByMainId(erpClassconfig.getId());

		//2.子表数据重新插入
		if(erpClassconfigBList!=null && erpClassconfigBList.size()>0) {
			for(ErpClassconfigB entity:erpClassconfigBList) {
				//外键设置
				entity.setPkH(erpClassconfig.getId());
				erpClassconfigBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		erpClassconfigBMapper.deleteByMainId(id);
		erpClassconfigMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			erpClassconfigBMapper.deleteByMainId(id.toString());
			erpClassconfigMapper.deleteById(id);
		}
	}
    @Override
    public boolean removeById(Serializable id) {
        /**删除数据，移除流程关联信息**/
        flowCommonService.delActBusiness(id.toString());
        return super.removeById(id);
    }
    /**
     * 流程处理完成后的回调
     * @param business 里面包含流程运行的现状信息，业务层可根据其信息判断，书写增强业务逻辑的代码，<br/>
     *                 1、比如将其中关键信息存入业务表，即可单表业务操作,否则需要关联flow_my_business表获取流程信息<br/>
     *                 2、比如在流程进行到某个节点时(business.taskId)，需要特别进行某些业务逻辑操作等等<br/>
     */
      @Override
      public void afterFlowHandle(FlowMyBusiness business) {

           business.getTaskNameId();//接下来审批的节点
           business.getValues();//前端传进来的参数
           business.getActStatus();//流程状态 ActStatus.java
           //....其他
           ErpClassconfig   erpClassconfig= this.getById(business.getDataId());
          // erpClassconfig.setActstatus(StatusEnum.getEnumKey(business.getActStatus()));
           // erpClassconfig.
             this.updateById(erpClassconfig);
        }


   /**
     * 根据业务id返回业务表单数据<br/>
     * @param dataId
     * @return
     */
        @Override
        public Object getBusinessDataById(String dataId) {
            return this.getById(dataId);
        }
    /**
     * 返回当前节点的流程变量
     * @param taskNameId 节点定义id
     * @param values 前端传入的变量，里面包含dataId
     * @return
     */
        @Override
        public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
            return null;
        }
    /**
     * 返回当前节点的候选人username
     * @param taskNameId 节点定义id
     * @param values 前端传入的变量，里面包含dataId
     * @return
     */
        @Override
        public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
            // 通过当前节点来判断下一个节点的候选人并写回到反参中，如果为null，流程模块会根据默认设置处理

            return null;
        }


        @Override
        public void relationAct(String dataId) {
             //flowCommonService.initActBusiness("实验授课申请：dataId"+dataId,dataId,"erpClassconfigService","process_tpho5a4q","process_tpho5a4q:4:32128f7c-ce77-11ec-843e-927841cf098b");
        }

}
